package org.andao.commons.lucene.index;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.andao.commons.lucene.core.LuceneServerFactory;
import org.andao.commons.lucene.core.SolrServerContext;
import org.andao.commons.lucene.core.assembler.BeanListAssembler;
import org.andao.commons.lucene.core.assembler.SolrBeanListAssembler;
import org.andao.commons.lucene.core.mapper.BeanMapper;
import org.andao.commons.lucene.exception.LuceneQueryException;
import org.andao.core.base.controller.BaseSupportController;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


/**
 * lucene搜索索引创建类，基于solr的实现
 * 
 * @author Seewo Software - Vick.liu (liuguanglin@cvte.cn)
 * 
 */
@Component("solrIndexCreator")
public class SolrIndexCreator implements LuceneIndexCreator {

	protected static Logger logger = LoggerFactory.getLogger(BaseSupportController.class);

	/**
	 * solrServer
	 */
	private SolrServer solrServer;

	public SolrIndexCreator() throws LuceneQueryException {
		solrServer = (SolrServer) LuceneServerFactory.getServer(new SolrServerContext());
	}

	@Override
	public void createIndexs(List<?> sourceList, String key, BeanMapper beanMapper) throws SolrServerException, IOException {
		createIndexs(sourceList, key, new SolrBeanListAssembler(beanMapper));
	}

	private void createIndexs(List<?> sourceList, String key, BeanListAssembler beanListAssembler) throws SolrServerException, IOException {
		logger.info("----> createIndexs start!");

		// 装配成 Collection<BasicIndexBean> 或Collection<SolrInputDocument>
		List<?> indexBeanList = (List<?>) beanListAssembler.assembleData(sourceList, key);

		try {
			for (Object obj : indexBeanList) {
				if (obj instanceof SolrInputDocument) {
					// 如果是动态域，则对象是SolrInputDocument的实例
					solrServer.add((SolrInputDocument) obj);
				} else {
					// 否则是定义好静态域
					solrServer.addBean(obj);
				}
				logger.info("----> creating index for " + obj.toString());
				Thread.sleep(1000);// 由于远程调用，所以需要缓冲时间，防止网络堵塞
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		solrServer.commit(true, true);

		logger.info("<---- createIndexs committed!");
	}

	@Override
	public void addIndex(Object obj) throws SolrServerException, IOException {
		logger.info("----> creating index for " + obj.toString());
		if (obj instanceof SolrInputDocument) {
			// 如果是动态域，则对象是SolrInputDocument的实例
			solrServer.add((SolrInputDocument) obj);
		} else {
			// 否则是定义好静态域
			solrServer.addBean(obj);
		}
		solrServer.commit(true, true);

		logger.info("<---- createIndexs committed!");
	}

	@Override
	public void updateIndex(String targetKey, Object newObj) throws IOException, SolrServerException {
		logger.info("----> updating index for " + newObj.toString());
		if (newObj instanceof SolrInputDocument) {
			// 如果是动态域，则对象是SolrInputDocument的实例
			solrServer.add((SolrInputDocument) newObj);
		} else {
			// 否则是定义好静态域
			solrServer.addBean(newObj);
		}
		solrServer.commit(true, true);

		logger.info("<---- update index committed!");
	}

	@Override
	public void updateIndexs(Map<String, Object> map) throws IOException, SolrServerException {
		for (Entry<String, Object> entry : map.entrySet()) {
			updateIndex(entry.getKey(), entry.getValue());
		}
	}

	@Override
	public void deleteIndex(Object obj) {
		// TODO 删除索引
	}

	@Override
	public void destroyIndex() throws SolrServerException, IOException {
		logger.info("----> start destroying indexs...");
		solrServer.deleteByQuery("*:*");
		solrServer.commit();
		logger.info("<---- All indexs destroyed.");
	}

}
